package com.zhiyou100.serice.Impl;

import com.zhiyou100.mapper.UserMapper;
import com.zhiyou100.model.User;
import com.zhiyou100.serice.UserSerice;
import com.zhiyou100.util.UtilS;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.MD5;

import java.util.Date;
import java.util.List;

@Service
public class UserSericeImpl implements UserSerice {

    @Autowired
    private UserMapper mapper;

    public User findUserByEmail(String email) {
        return mapper.findUserByEmail(email);
    }

    public List<String> findRoleById(Integer id) {
        return mapper.findRoleById(id);
    }

    public List<String> findPermissionById(Integer id) {
        return mapper.findPermissionById(id);
    }

    public boolean right(User user,String pwd_action) {
        Integer id = mapper.findByPhoneNum(user.getPhoneNum());
        Integer idtuo = mapper.findByEmail(user.getEmail());
        System.out.println("UserSerice中user对象的信息 : "+user);
        System.out.println("UserSerice中pwd_action的信息 : "+pwd_action);
        if((id != null && !id.equals(0))||(idtuo !=null && !idtuo.equals(0))|| user.getPassword() == null || pwd_action ==null || !user.getPassword().equals(pwd_action)){
            return false;
        }
        user.setCalt(UtilS.getCalt());
        String md = new Md5Hash(user.getPassword(),user.getCalt(),10).toString();
        user.setPassword(md);
        user.setCreateTime(new Date());
        mapper.insert(user);
        return true;
    }

    public User findById(Integer id) {
        return mapper.findById(id);
    }

}
